import {
	getObj,
	getContractApproveList
} from "@/views/wy-lessor/rentalSociety/api/contract.js";
import { getObj as getClientManage } from "@/views/wy-lessor/landlord/api/lessor.js";

import pdf from "vue-pdf";
//import uploadMedia from '@/components/upload-media'
// 引入vuex
import { mapState } from "vuex";
import approvalRecord from "@/components/approval-record";

import contractChange from "./components/contractChange.vue";
import contractDetails from "./components/contractDetails.vue";
import contractRenew from "./components/contractRenew.vue";
import contractBack from "./components/contractBack.vue";
import uploadMedia from "@/components/upload-media";

export default {
	name: "contract-view",
	components: {
		pdf,
		approvalRecord,
		contractDetails,
		contractChange,
		contractRenew,
		contractBack,
		uploadMedia
	},
	data() {
		return {
			recordingLlist: [],
			viewDialogVisible: false,
			activeName: "first",
			formData: {}, //合同总信息
			contractDetail: "", //合同明细
			lessorContractProfee: "", //合同费用
			lessorContractId: "", //合同id
			customerType: "", //出租方类型
			industryCategory: " ", //行业类别
			certificateType: "", //证件类型
			//文件上传
			uploadFileList: "",
			//合同预览
			pageNum: 1,
			pageTotalNum: "",
			page: "",
			numPages: 1,
			propertyNames: "",
			dialogContractVisible: false,
			pdfUrl: "",
			rentPriceRejustType: [], //调价方式
			rentCertificateType: [], //证件类型
			rentIndustryCategory: [], //行业类别
			rentSigningMethod: [], //签订方式
			rentFeeType: [], //收费方式
			//出租方内容展示
			//propForm: '',
			propForm: {
				lessorType: "",
				lessorName: ""
			},
			//合同内容展示
			//formData:'',
			propertyInformation: [],
			rentFeeInformation: [],
			contractFee: [],
			//字典项匹配
			lessee: "",
			providerType: [], //供应商类型
			provider: "",
			commercialCertificateType: [], //工商证件类型
			commercialCertificate: "",
			personageCertificateType: [], //个人证件类型
			personageCertificate: "",
			subType: "", //合同细类
			type: "", //合同类型
			signType: "", //签订方式
			feeType: "", //计租方式
			approveType: "", //审批方式
			administrativeOffice: "" //管理所
		};
	},
	computed: mapState({
		//调价方式
		rent_price_rejust_type: state => {
			let data =
				state.dict.find(item => item.type === "rent_price_rejust_type") || {};
			return data.children || [];
		},
		//证件类型
		rent_certificate_type: state => {
			let data =
				state.dict.find(item => item.type === "rent_certificate_type") || {};
			return data.children || [];
		},
		//签订方式
		rent_signing_method: state => {
			let data =
				state.dict.find(item => item.type === "rent_signing_method") || {};
			return data.children || [];
		},
		//收费方式
		rent_fee_type: state => {
			let data = state.dict.find(item => item.type === "rent_fee_type") || {};
			return data.children || [];
		},
		//审批方式
		rent_contract_approve_type: state => {
			let data =
				state.dict.find(item => item.type === "rent_contract_approve_type") ||
				{};
			return data.children || [];
		},
		//管理所
		administrative_office: state => {
			let data =
				state.dict.find(item => item.type === "administrative_office") || {};
			return data.children || [];
		},
		lessee_type: state => {
			let data = state.dict.find(item => item.type === "lessee_type") || {};
			return data.children || [];
		},
		provider_type: state => {
			let data = state.dict.find(item => item.type === "provider_type") || {};
			return data.children || [];
		},
		personage_certificate_type: state => {
			let data =
				state.dict.find(item => item.type === "personage_certificate_type") ||
				{};
			return data.children || [];
		},
		commercial_certificate_type: state => {
			let data =
				state.dict.find(item => item.type === "commercial_certificate_type") ||
				{};
			return data.children || [];
		},
		rent_contract_sub_type: state => {
			let data =
				state.dict.find(item => item.type === "rent_contract_sub_type") || {};
			return data.children || [];
		},
		rent_contract_type: state => {
			let data =
				state.dict.find(item => item.type === "rent_contract_type") || {};
			return data.children || [];
		}
	}),

	watch: {
		//$route:"changeView"
	},

	methods: {
		//获取审批列表
		async getApprovalRecord(id) {
			const res = await getContractApproveList(id);
			if (res.data.code == 0) this.recordingLlist = res.data.data;
		},
		//查询表单数据
		async show(contractId) {
			//改为了弹出框模式
			this.viewDialogVisible = true;

			this.lessorContractId = contractId;
			//if (this.$route.query.id != null) {
			//    this.lessorContractId = this.$route.query.id;
			//}
			//alert(this.lessorContractId);
			const response = await getObj(this.lessorContractId);
			//alert(JSON.stringify(response));
			let formData = response.data.data;
			this.formData = formData;
			this.rentFeeInformation = formData.lessorContractDetails;
			this.propertyInformation = formData.lessorContractRooms;
			this.contractFee = formData.lessorContractProfees;

			//合同预览
			this.pdfUrl = formData.contractUrl;
			this.propertyNames = formData.propertyNames;

			//获得出租方信息
			if (this.formData.customerId) {
				const propForm = await getClientManage(this.formData.customerId);
				//if( propForm.data && propForm.data.data){
				this.propForm = propForm.data.data;
				//}
				this.showlessee();
			}
			//查看附件
			this.$refs.lessorContractFileUpload.getFiles(
				this.formData.lessorContractId
			);

			//5个子表
			//收费明细
			//alert();

			// cqg+ 2020/12/07 11:34

			if (
				this.$refs.contractDetails &&
				this.$refs.contractDetails.preViewFee &&
				this.rentFeeInformation &&
				this.rentFeeInformation.length > 0
			)
				this.$refs.contractDetails.preViewFee(contractId, this.formData);
			if (this.$refs.contractChange?.loadHisList)
				this.$refs.contractChange.loadHisList(contractId);
			if (this.$refs.contractRenew?.loadRenewList)
				this.$refs.contractRenew.loadRenewList(contractId);
			if (this.$refs.contractBack?.loadBackInfo)
				this.$refs.contractBack.loadBackInfo(contractId);
			this.getApprovalRecord(contractId);
		},
		//展示证件类型
		showlessee() {
			//遍历字典，获得数据值对应中文描述
			for (var j = 0, len = this.lessee_type.length; j < len; j++) {
				if (
					this.propForm != null &&
					this.lessee_type[j].value == this.propForm.lessorType
				) {
					this.lessee = this.lessee_type[j].label;
				}
			}
			for (var j = 0, len = this.provider_type.length; j < len; j++) {
				if (
					this.propForm != null &&
					this.provider_type[j].value == this.propForm.providerType
				) {
					this.provider = this.provider_type[j].label;
				}
			}
			for (
				var j = 0, len = this.personage_certificate_type.length;
				j < len;
				j++
			) {
				if (
					this.propForm != null &&
					this.personage_certificate_type[j].value ==
						this.propForm.certificateType
				) {
					this.personageCertificate = this.personage_certificate_type[j].label;
				}
			}
			this.personageCertificate = "身份证";
			for (
				var j = 0, len = this.commercial_certificate_type.length;
				j < len;
				j++
			) {
				if (
					this.propForm != null &&
					this.commercial_certificate_type[j].value ==
						this.propForm.certificateType
				) {
					this.commercialCertificate = this.commercial_certificate_type[
						j
					].label;
				}
			}
			for (var j = 0, len = this.rent_signing_method.length; j < len; j++) {
				if (this.rent_signing_method[j].value == this.formData.signType) {
					this.signType = this.rent_signing_method[j].label;
				}
			}
			for (var j = 0, len = this.rent_fee_type.length; j < len; j++) {
				if (this.rent_fee_type[j].value == this.formData.feeType) {
					this.feeType = this.rent_fee_type[j].label;
				}
			}
			for (
				var j = 0, len = this.rent_contract_approve_type.length;
				j < len;
				j++
			) {
				if (
					this.rent_contract_approve_type[j].value == this.formData.approveType
				) {
					this.approveType = this.rent_contract_approve_type[j].label;
				}
			}
			for (var j = 0, len = this.administrative_office.length; j < len; j++) {
				if (
					this.administrative_office[j].value ==
					this.formData.administrativeOffice
				) {
					this.administrativeOffice = this.administrative_office[j].label;
				}
			}
			for (var j = 0, len = this.rent_contract_sub_type.length; j < len; j++) {
				if (this.rent_contract_sub_type[j].value == this.formData.subType) {
					this.subType = this.rent_contract_sub_type[j].label;
				}
			}
			for (var j = 0, len = this.rent_contract_type.length; j < len; j++) {
				if (this.rent_contract_type[j].value == this.formData.type) {
					this.type = this.rent_contract_type[j].label;
				}
			}
		},
		//合同预览
		//上一页
		prePage() {
			var p = this.pageNum;
			p = p > 1 ? p - 1 : this.pageTotalNum;
			this.pageNum = p;
		},
		//下一页
		nextPage() {
			var p = this.pageNum;
			p = p < this.pageTotalNum ? p + 1 : 1;
			this.pageNum = p;
		},
		handleClose(done) {
			this.$confirm("确认关闭？")
				.then(_ => {
					done();
				})
				.catch(_ => {});
		},
		close() {
			//alert("aa");
			this.viewDialogVisible = false;
		}
	}
};
